//% color="#4169E1" iconWidth=50 iconHeight=40
namespace DFRobot_BC20{

    //% block="NB-IoT initialization I2C mode Address:[ADDR]  " blockType="command" 
    //% ADDR.shadow="dropdown" ADDR.options="ADDR"
     
    export function DFRobot_BC20_I2cInit(parameter: any, block: any) {
         let addr = parameter.ADDR.code;
         //let bo = parameter.BO.code;

     

        Generator.addInclude("DFRobot_BC20_Gravity", "#include <DFRobot_BC20_Gravity.h>");
    
        Generator.addObject("DFRobot_BC20_Gravity","DFRobot_BC20_IIC",`myBC20(${addr});`);
        
        //Generator.addSetup("DFRobot_BC20_Gravity",`Serial.begin(${bo});`);
      
    }
     //% board=" arduino,leonardo,arduinonano,mega2560"
     //% block="NB-IoT initialization [HS] mode RX (green): [RX] TX (blue): [TX]  " blockType="command" 
    //% HS.shadow="dropdown" HS.options="HS"
    //% RX.shadow="dropdown" RX.options="RX"
    //% TX.shadow="dropdown" TX.options="TX" 
    
    export function DFRobot_BC20_HsInit(parameter: any, block: any) {
        //let bo = parameter.BO.code;
         
       Generator.addInclude("DFRobot_BC20_Gravity", "#include <DFRobot_BC20_Gravity.h>");

      
       Generator.addObject("DFRobot_BC20_Gravity1","DFRobot_BC20_Serial",`myBC20(&Serial);`);

       //Generator.addSetup("DFRobot_BC20_Gravity",`Serial.begin(${bo});`);
       
   }
   //% board=" arduino,leonardo,arduinonano,mega2560"
   //% block="NB-IoT initialization [SS] mode RX (green): [SRX] TX (blue): [STX]" blockType="command" 
    //% SS.shadow="dropdown" SS.options="SS"
    //% SRX.shadow="dropdown" SRX.options="SRX"    
    //% STX.shadow="dropdown" STX.options="STX"
    
   export function DFRobot_BC20_SsInit(parameter: any, block: any) {
         
        let rx = parameter.SRX.code;
        let tx = parameter.STX.code;
       // let bo = parameter.BO.code;
       Generator.addInclude("DFRobot_BC20_Gravity", "#include <DFRobot_BC20_Gravity.h>");

       Generator.addObject("DFRobot_BC20_Gravity","SoftwareSerial",`ss(${rx}, ${tx});`);
       Generator.addObject("DFRobot_BC20_Gravity1","DFRobot_BC20_SW_Serial",`myBC20(&ss);`);

      // Generator.addSetup("DFRobot_BC20_Gravity",`Serial.begin(${bo});`);
}

  //% block="---"
  export function noteSep1() {

  }
//% block="NB-IoT[CL]RGB lamp: [RGB] " blockType="command" 
//% CL.shadow="dropdown" CL.options="CL"
//% RGB.shadow="dropdown" RGB.options="RGB"
export function DFRobot_BC20RGB(parameter: any, block: any) { 
  let cl=parameter.CL.code;
  let rgb=parameter.RGB.code;
 
  Generator.addSetup( `changeColor${rgb}`,`myBC20.changeColor(${rgb});`);

  Generator.addCode( `myBC20.LED_${cl}();`);

}
//% block="NB-IoT to obtain NB signal strength  " blockType="reporter" 

export function DFRobot_BC20GetSignal(parameter: any, block: any) { 
  
 
  Generator.addSetup( "powerOn1",`myBC20.powerOn1();`);
  Generator.addSetup( "check",`myBC20.check();`);
  Generator.addCode( [`myBC20.NB_Signal_Fun()` ,Generator.ORDER_UNARY_POSTFIX]);

}

//% block="NB-IoT Get current time of cellular base station: [TIME]" blockType="reporter" 
//% TIME.shadow="dropdown" TIME.options="TIME"
export function DFRobot_BC20GetTime(parameter: any, block: any) { 
  let time=parameter.TIME.code;
 
  Generator.addSetup( "powerOn1",`myBC20.powerOn1();`);
  Generator.addSetup( "check",`myBC20.check();`);
  Generator.addCode( [`myBC20.getLocalTime(${time})`,Generator.ORDER_UNARY_POSTFIX]);

}
//% block="---"
export function noteSep3() {

}
//% block="NB-IoT gets location data once  " blockType="command" 

export function DFRobot_BC20GetpositionInit(parameter: any, block: any) { 


Generator.addSetup( "powerOn1",`myBC20.powerOn1();`);
Generator.addSetup( "check",`myBC20.GPS();`);

Generator.addCode( `myBC20.getQGNSSRD2();`);
}

//% block="NB-IoT Get detailed location information: [POSITION] " blockType="reporter"
//% POSITION.shadow="dropdown" POSITION.options="POSITION" 
export function DFRobot_BC20Getposition(parameter: any, block: any) { 
  let ps=parameter.POSITION.code;
  
/*  if(`${ps}`==='1'){
  
  Generator.addCode( `myBC20.Display_Location_Information1(${ps})`);

 } else if(`${ps}`==='2'){
   
  Generator.addCode( `myBC20.Display_Location_Information(${ps})`);

 }else if(`${ps}`==='3'){

  Generator.addCode( `myBC20.Display_Location_Information1(${ps})`);

 }else if(`${ps}`==='4'){

  Generator.addCode( `myBC20.Display_Location_Information(${ps})`);

 } else if(`${ps}`==='5'){

  Generator.addCode( `myBC20.Display_Location_Information1(${ps})`);

 }else if(`${ps}`==='6'){

  Generator.addCode( `myBC20.Display_Location_Information1(${ps})`);

 } else if(`${ps}`==='7'){

  Generator.addCode( `myBC20.Display_Location_Information1(${ps})`);

 } else if(`${ps}`==='8'){

  Generator.addCode( `myBC20.Display_Location_Information(${ps})`);

 }else if(`${ps}`==='9'){

  Generator.addCode( `myBC20.Display_Location_Information(${ps})`);

 }else if(`${ps}`==='10'){

  Generator.addCode( `myBC20.Display_Location_Information(${ps})`);

 }else if(`${ps}`==='11'){

  Generator.addCode( `myBC20.Display_Location_Information(${ps})`);

 }*/
 
  
  Generator.addCode( [`myBC20.Display_Location_Information(${ps})`,Generator.ORDER_UNARY_POSTFIX]);


 

}
    //% block="---"
    export function noteSep() {

    }
    //% block="NB-IoT Initialize Easy-IoT server: [SERVER] Iot_id: [ID] Iot_pwd: [PWD]" blockType="command" 
    //% SERVER.shadow="dropdown" SERVER.options="SERVER"
     
    //% ID.shadow="string" ID.defl="gwjCEokMg"
    //% PWD.shadow="string" PWD.defl="RwjCEokMgz"
    export function DFRobot_BC20IotServer(parameter: any, block: any) { 
    
      let server = parameter.SERVER.code;
      
      let id = parameter.ID.code;
      let pwd = parameter.PWD.code;
     
      /*Generator.addObject("EasyIot_SERVER","char*",`EasyIot_SERVER  = ${server};`);
      Generator.addObject("PORT","char*",`PORT  = ${port};`);*/

      Generator.addSetup( "powerOn1",`myBC20.powerOn1();`);
      Generator.addSetup( "check",`myBC20.check();`);
    //  Generator.addSetup( "Serial",`Serial.println("Connecting to DFRobot Easy-IoT");`);
      Generator.addSetup("setServer",`myBC20.setServer("${server}", "1883");`);
      Generator.addSetup("ConnectCloud",`myBC20.ConnectCloudsend1("1234",${id},${pwd});`);
 
 }
    //% block="NB-IoT subscribe to Topic once: [Topic]" blockType="command" 
     //% Topic.shadow="string" Topic.defl="wQ6TWV_Wg"
     export function DFRobot_BC20IotServertopic(parameter: any, block: any) { 

      let topic = parameter.Topic.code;
      //Generator.addSetup("setServertopic",`myBC20.topic(${topic});`);
      Generator.addSetup(`setServertopic${topic}`,`myBC20.topic(${topic});`);
    
    }


  /*  //% block="DFRobot_BC20Iot Iot_id [ID] Iot_pwd [PWD]pubTopic [Topic]" blockType="command" 
    //% ID.shadow="string" ID.defl="00VdghAWg"
    //% CID.shadow="string" CID.defl="1234"
    //% PWD.shadow="string" PWD.defl="0AVdg20WRz"
    //% Topic.shadow="string" Topic.defl="wQ6TWV_Wg"
    export function DFRobot_BC20Iot(parameter: any, block: any) { 
        let id = parameter.ID.code;
        let cid = parameter.CID.code;
        let pwd = parameter.PWD.code;
        let tp = parameter.Topic.code;

        
         
        Generator.addObject("Iot_id","char*",`Iot_id = ${id};`);
        Generator.addObject("Client_ID","char*",`Client_ID   = ${cid};`);
        Generator.addObject("Iot_pwd","char*",`Iot_pwd     = ${pwd};`);
        Generator.addObject("pubTopic","char*",`pubTopic  = ${tp};`);

        
         

       
      //  Generator.addSetup("println",`Serial.println("Server is available!");`);
        
        Generator.addSetup("ConnectCloud",`myBC20.ConnectCloudsend1("",${id},${pwd},${tp});`);
       
   
   }
   */

  
    //% block="NB-IoT Send message [MESS] to Topic [Topic]" blockType="command" 
     //% MESS.shadow="string" SERVER.MESS="hello"
    //% Topic.shadow="string" Topic.defl="wQ6TWV_Wg"
    export function DFRobot_BC20Iotsend(parameter: any, block: any) { 
        let mess = parameter.MESS.code;
        let tp = parameter.Topic.code;
 
        Generator.addCode(`myBC20.publish(${tp},${mess});`);
        
   
    }

  /*  //% block="DFRobot_BC20Iotreceive " blockType="command" 
    
    export function DFRobot_BC20Iotreceive(parameter: any, block: any) { 
        
 

       // Generator.addSetup("ConnectCloudreceive",`myBC20.ConnectCloudreceive(${tp});`);

        //Generator.addEvent("1","void","callback","char * topic, uint8_t * payload, unsigned int len");

        //Generator.addSetup("setCallback",`myBC20.setCallback(callback);`);

       
        //Generator.addCode(`Serial.print("Recevice [Topic:");\n\tSerial.print(topic);\n\tSerial.print("] ");\n\tfor (int i = 0; i < len; i++) {\n\t\tSerial.print((char)payload[i]);\n\t}\n\tSerial.println();\n\tString receivedData = payload;`);
    
    
    }
*/

       //% block="NB-IoT get data once " blockType="command" 
     
       export function DFRobot_BC20IotreceiveMessLOOP(parameter: any, block: any) { 
        
       
        Generator.addCode(`myBC20.loop();`);
        
    
    }
     //% block="Did NB-IoT receive the message? " blockType="boolean" 
     
    export function DFRobot_BC20IotreceiveMess(parameter: any, block: any) { 
         
       
        Generator.addCode(`myBC20.flag1==1`);
        
    
    }

   //% block="NB-IoT Topic for reading messages " blockType="reporter" 
       
    export function DFRobot_BC20IotreceiveMesstopic(parameter: any, block: any) { 
         
       
      Generator.addCode([`myBC20.read1topic1()`,Generator.ORDER_UNARY_POSTFIX]);
      
  
  } 
    //% block="NB-IoT read message content " blockType="reporter" 
       
    export function DFRobot_BC20IotreceiveMessdata(parameter: any, block: any) { 
         
       
      Generator.addCode([`myBC20.readdata2()`,Generator.ORDER_UNARY_POSTFIX]);
      
  
  } 
  //% block="---"
  export function noteSep2() {

  }
    //% block="DFRobot_BC20SerialDBG  " blockType="command" 
   
/*  export function DFRobot_BC20SerialDBG (parameter: any, block: any) { 
        
       
        Generator.addSetup( "powerOn1",`myBC20.powerOn1();`);
        
        Generator.addSetup( "powerOn12",`Serial.println("Enter AT commands:");`);
        Generator.addCode( `myBC20.available1();`);
   
   }
 */  

}